package com.lb.activity.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lb.activity.mapper.ExaminePermissionMapper;
import com.lb.activity.mapper.ExamineRoleMapper;
import com.lb.activity.mapper.ExamineRolePermissionMapper;
import com.lb.activity.po.ExaminePermission;
import com.lb.activity.po.ExamineRole;
import com.lb.activity.po.ExamineRolePermission;
import com.lb.activity.po.RolePermission;
import com.lb.activity.service.ExamineRoleService;
import com.lb.activity.util.BuildTree;
import com.lb.activity.util.PermissionTree;
import com.lb.activity.util.ResultUtil;
import com.lb.activity.util.SnowflakeIdWorker;
import com.lb.activity.util.emumdata.DataEnum;
import com.lb.activity.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liubin
 */
@Service
public class ExamineRoleServiceImpl implements ExamineRoleService {


    @Autowired
    private SnowflakeIdWorker idWorker;

    @Autowired
    private ExamineRoleMapper examineRoleMapper;

    @Autowired
    private ExaminePermissionMapper examinePermissionMapper;

    @Autowired
    private ExamineRolePermissionMapper examineRolePermissionMapper;

    @Override
    public ResultUtil list(RoleVo roleVo) {
        PageHelper.startPage(roleVo.getPage(),roleVo.getLimit());
        Example example = new Example(ExamineRole.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andNotEqualTo("status", DataEnum.DELETE_STATUS.getStatus());
        List<ExamineRole> roles = examineRoleMapper.selectByExample(example);
        PageInfo<ExamineRole> pageInfo = new PageInfo<>(roles);
        return ResultUtil.build(0, "", Integer.parseInt(String.valueOf(pageInfo.getTotal())), pageInfo.getList());
    }

    @Override
    public ResultUtil add(ExamineRole role) {
        role.setId(idWorker.nextId());
        role.setStatus(DataEnum.INSERT_STATUS.getStatus());
        // 执行插入
        examineRoleMapper.insertSelective(role);
        return ResultUtil.ok();
    }

    @Override
    public ExamineRole getOne(Long id) {
        return examineRoleMapper.selectByPrimaryKey(id);
    }

    @Override
    public ResultUtil update(ExamineRole role) {
        examineRoleMapper.updateByPrimaryKeySelective(role);
        return ResultUtil.ok();
    }

    @Override
    public ResultUtil delete(Long id) {
        ExamineRole role = ExamineRole.builder().id(id).status(DataEnum.DELETE_STATUS.getStatus()).build();
        examineRoleMapper.updateByPrimaryKeySelective(role);
        return ResultUtil.ok();
    }

    @Override
    public ResultUtil changeStatus(Long id, boolean boolStatus) {
        Long status = DataEnum.INSERT_STATUS.getStatus();
        // 如果为true ,为 lock 即 禁用
        if(boolStatus){
            status = DataEnum.FORBIDDEN_STATUS.getStatus();
        }
        ExamineRole role = ExamineRole.builder().id(id).status(status).build();
        examineRoleMapper.updateByPrimaryKeySelective(role);
        return ResultUtil.ok();
    }

    @Override
    public PermissionTree<ExaminePermission> getRoleTree(Long roleId) {
        // 查询所有的权限
        Example example = new Example(ExaminePermission.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andNotEqualTo("status",DataEnum.DELETE_STATUS.getStatus());
        List<ExaminePermission> allPermissions = examinePermissionMapper.selectByExample(example);
        // 查询角色下的权限
        List<Long> permissions = examineRolePermissionMapper.selectPermissionByRoleId(roleId);

        List<PermissionTree<ExaminePermission>> trees = new ArrayList<>();

        for (ExaminePermission permission : allPermissions) {
            PermissionTree<ExaminePermission> tree = new PermissionTree<>();
            tree.setId(permission.getId().toString());
            tree.setParentId("0");
            tree.setText(permission.getName());
            Map<String, Object> state = new HashMap<>(1);
            Long permissionId = permission.getId();
            if (permissions!=null&&permissions.size()>0&&permissions.contains(permissionId)) {
                state.put("selected", true);
            } else {
                state.put("selected", false);
            }
            tree.setState(state);
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        PermissionTree<ExaminePermission> t = BuildTree.build(trees);
        return t;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modifyRoleMenus(Long id, String[] menuIds) {
        examineRolePermissionMapper.removeByRoleId(id);
        List<ExamineRolePermission> storeRolePermissions = new ArrayList<>();
        for (String  menuId: menuIds) {
            ExamineRolePermission storeRolePermission= new ExamineRolePermission();
            storeRolePermission.setId(idWorker.nextId());
            storeRolePermission.setRoleid(id);
            storeRolePermission.setPermissionid(Long.valueOf(menuId));
            storeRolePermission.setStatus(DataEnum.INSERT_STATUS.getStatus());
            storeRolePermissions.add(storeRolePermission);
        }
        if (storeRolePermissions.size() > 0) {
            examineRolePermissionMapper.batchSave(storeRolePermissions);
        }

    }

    @Override
    public ResultUtil allRoles() {
        Example example = new Example(ExamineRole.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andNotEqualTo("status", DataEnum.DELETE_STATUS.getStatus());
        List<ExamineRole> roles = examineRoleMapper.selectByExample(example);
        return ResultUtil.ok(roles);
    }
}
